package com.yangtzeu.jwc.activity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;

import com.yangtzeu.jwc.business.AppBusiness;
import com.yangtzeu.jwc.businessservice.BusinessHttpCallBack;
import com.yangtzeu.jwc.businessservice.impl.StudentServiceImpl;
import com.yangtzeu.jwc.customview.CustomeProgressDialog;
import com.yangtzeu.jwc.domain.Student;
import com.yangtzeu.jwc.utils.G;
import com.zhangdi.jwc.R;
import com.zjsx.lib.utils.StrUtil;
import com.zjsx.lib.utils.T;

/**
 * 登陆Activity
 */
@SuppressLint("HandlerLeak")
public class LoginActivity extends BaseActivity {
    private EditText et_username;
    private EditText et_password;
    private Button bt_login;
    private ProgressDialog dialog;
    private CheckBox savePwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	// 得到数据库中保存的用户信息
	Student student = StudentServiceImpl.getInstance().getSavedStudent(getApplicationContext());
	if (student != null) {
	    et_username.setText(student.getSno());
	    et_password.setText(student.getSpassword());
	}
    }

    @Override
    public void setView() {
	super.setView();
	setContentView(R.layout.activity_login);
    }

    @Override
    public void initActionBar() {
	//super.initActionBar();
	setSimpleActionBar("返回", "", "登陆教务处", null);
    }

    @Override
    public void initView() {
	super.initView();
	et_username = (EditText) findViewById(R.id.login_edit_account);
	et_password = (EditText) findViewById(R.id.login_edit_pwd);
	bt_login = (Button) findViewById(R.id.login_btn_login);
	savePwd = (CheckBox) findViewById(R.id.login_cb_savepwd);
    }

    @Override
    public void initListener() {
	super.initListener();
	savePwd.setOnCheckedChangeListener(new savePwdOnCheckListener());
	bt_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
	super.onClick(v);
	if (v.getId() == R.id.login_btn_login) {
	    login();
	}
    }

    public void login() {
	String username = et_username.getText().toString();
	String password = et_password.getText().toString();
	if(StrUtil.isEmpty(username)){
	    T.showShort(this, "请输入您的学号!");
	    return;
	}
	if(StrUtil.isEmpty(password)){
	    T.showShort(this, "请输入登陆密码!");
	    return;
	}
	dialog = CustomeProgressDialog.show(LoginActivity.this, "正在登陆...");
	StudentServiceImpl.getInstance().login(username, password, "1", new BusinessHttpCallBack() {

	    @Override
	    public void onProgress(int bytesWritten, int totalSize) {

	    }

	    @Override
	    public void onFailure(byte[] responseBody, String failuremsg, boolean isNoticeDialog) {
		dialog.dismiss();
		Toast.makeText(getApplicationContext(), failuremsg, Toast.LENGTH_LONG).show();
	    }

	    @Override
	    public void onSuccess(byte[] responseBody, String successMsg, boolean isNoticeDialog, Object obj) {
		dialog.dismiss();
		Toast.makeText(getApplicationContext(), successMsg, Toast.LENGTH_SHORT).show();
		if (savePwd.isChecked()) {
		    StudentServiceImpl.getInstance().saveStudent(getApplicationContext(), MainActivity.student.getSno(), MainActivity.student.getSpassword());
		}
		finish();
	    }

	});

    }

    /*
     * 保存密码的选项框的选中事件
     */
    private class savePwdOnCheckListener implements OnCheckedChangeListener {

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
	    if (isChecked) {

	    } else {
		// 取消保存密码的勾的时候清空保存的用户信息.
		StudentServiceImpl.getInstance().clearStudent(getApplicationContext());
	    }
	}

    }
}
